package org.framework.helper;

import org.framework.utils.ReflectUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * bean 容器助手类
 * @Author xuelongjiang
 */
public class BeanHelper {


    private static final Map<Class<?>,Object> BEAN_MAP = new HashMap<Class<?>,Object>();

    static {
        Set<Class<?>> classSet = ClassHelper.getClassSet();
        for(Class<?> cls:classSet){
            Object obj = ReflectUtil.newInstance(cls);
            BEAN_MAP.put(cls,obj);
        }
    }

    /**
     * 获取bean映射
     * @return
     */
    public static Map<Class<?>,Object> getBeanMap(){
        return  BEAN_MAP;
    }

    /**
     * 获取bean实例
     */
    public static <T> T getBean(Class<T> cls){

        if(!BEAN_MAP.containsKey(cls)){
            throw  new RuntimeException("can not get bean by class:"+cls);
        }
        return (T)BEAN_MAP.get(cls);
    }

}
